package com.huanghj.base.hessianAnnotations.util;


import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Map;
import java.util.Properties;

import com.google.common.collect.Maps;
import com.google.common.io.Resources;

/**
 * @author Hungel.zou
 * @time 2015年6月18日 下午3:25:01
 */
public class PropertiesUtil {
	
	public static final String COMMONBASE = "base.properties";
	public static final String COMMON = "base.properties";
	
	
	
	public static Map<String, String> readProperties(String resourceName) {
		Properties properties = new Properties();
		URL resource = Resources.getResource(resourceName);
		try {
			properties.load(new InputStreamReader(resource.openStream(),
					"UTF-8"));
		} catch (Exception e) {
			// TODO
		}
		return Maps.fromProperties(properties);
	}
	
	public static Properties getProperties(String propertyName){
		Properties pro = new Properties();
		InputStream fis = null;
		try{
			fis = PropertiesUtil.class.getClassLoader().getResourceAsStream((propertyName));
			if (fis != null){
				pro.load(fis);
				return pro;
			}
		} catch(Exception e){
			e.printStackTrace();
		} finally{
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
	
	public static Properties getCommonProperties(){
		return getProperties(COMMON);
	}
	
	public static String getProperty(String key) {
		if (getCommonProperties() != null) {
			return getCommonProperties().getProperty(key);
		}
		return null;
	}
	
	public static void main(String[] args){
		PropertiesUtil u = new PropertiesUtil();
		System.out.println(u.readProperties("base.properties"));
	}
}
